<template>
  <div id="train-refund-orders" class="mt-5">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb pb-0">
        <li class="breadcrumb-item active ms-1" aria-current="page">
          火车票退票
        </li>
      </ol>
    </nav>
    <template v-if="searchOption">
      <div class="card-body mb-0">
        <form>
          <div class="col-1">
            <my-date-picker
              id="beginDate"
              v-model="beginDate"
              name="sc.beginDate"
              placeholder="开始日期"
            ></my-date-picker>
          </div>
          <div class="col-1">
            <my-date-picker
              id="endDate"
              v-model="endDate"
              name="sc.endDate"
              placeholder="截止日期"
            ></my-date-picker>
          </div>
          <div class="col-1">
            <input
              type="textfield"
              class="form-control"
              size="6"
              placeholder="订单号"
              v-model.trim="orderNo"
            />
          </div>
          <div class="col-1">
            <my-select-customer
              :status="1"
              :customerId.sync="customerId"
              :enterpriseType="1"
            ></my-select-customer>
          </div>
          <div class="col-1">
            <input
              type="textfield"
              class="form-control ms-1"
              size="6"
              placeholder="乘客"
              v-model.trim="name"
            />
          </div>
          <div class="col-1">
            <my-date-picker
              id="ddate0"
              v-model="ddate"
              placeholder="出发日期"
            ></my-date-picker>
          </div>
          <div class="col-1">
            <select
              class="form-control form-control-sm"
              v-model.number="status"
              name="sc.status"
            >
              <option value="-1">状态</option>
              <option value="0">待处理</option>
              <option value="1">处理中</option>
              <option value="2">已完成</option>
              <option value="4">已取消</option>
            </select>
          </div>
          <div class="col-1">
            <select class="form-select" v-model.number="sc.pageSize">
              <option value="25">25</option>
              <option value="50">50</option>
              <option value="100">100</option>
            </select>
          </div>

          <button
            type="button"
            class="btn btn-primary ms-1"
            @click.stop="search()"
          >
            确定
          </button>
          <button
            type="button"
            class="btn btn-sm btn-secondary ms-1"
            @click.stop="reset()"
          >
            重置
          </button>
        </form>
      </div>
    </template>
    <template v-else>
      <table class="table table-response table-sm table-striped">
        <thead>
          <tr>
            <th class="d-none d-lg-table-cell">订单号</th>
            <th class="d-none d-lg-table-cell">外部订单号</th>
            <th class="d-none d-md-table-cell">公司</th>
            <th>乘客</th>
            <th>出发</th>
            <th>到达</th>
            <th>出发日期</th>
            <th class="d-none d-md-table-cell">车次</th>
            <th class="d-none d-md-table-cell">等级</th>
            <th class="d-none d-md-table-cell">价格</th>
            <th class="d-none d-md-table-cell">成本</th>
            <th class="d-none d-md-table-cell">服务费</th>
            <th class="d-none d-md-table-cell">预订员</th>

            <th class="d-none d-md-table-cell">生成时间</th>
            <th class="d-none d-md-table-cell">状态</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <template v-for="(info, index) in dataList">
            <tr
              :class="{ 'bg-info text-white': info.status !== 2 }"
              :key="index"
              @click.stop="showDetail(info.id)"
            >
              <td class="d-none d-lg-table-cell">{{ info.orderNo }}</td>
              <td class="d-none d-lg-table-cell">{{ info.externalOrderNo }}</td>
              <td class="d-none d-md-table-cell">{{ info.customerName }}</td>
              <td>{{ getShortName(info.passengerName, 6) }}</td>
              <td>{{ info.dcity }}</td>
              <td>{{ info.acity }}</td>
              <td>{{ info.ddate.substring(5) }}</td>
              <td class="d-none d-md-table-cell">{{ info.trainNo }}</td>
              <td class="d-none d-md-table-cell">{{ info.seatClass }}</td>
              <td class="d-none d-md-table-cell">{{ info.price }}</td>
              <td class="d-none d-md-table-cell">{{ info.cost }}</td>
              <td class="d-none d-md-table-cell">{{ info.serviceCharge }}</td>
              <td class="d-none d-md-table-cell">{{ info.operator }}</td>

              <td class="d-none d-md-table-cell">{{ info.createTime }}</td>
              <td class="d-none d-md-table-cell">
                {{ getStatusDesc(info.status) }}
              </td>
              <td>
                <i class="mdi mdi-arrow-right fa-2"></i>
              </td>
            </tr>
          </template>
        </tbody>
      </table>

      <nav id="pagination-box" class="float-end">
        <my-pagination
          name="pagination"
          :row-count="sc.rowCount"
          :page-total="sc.pageTotal"
          :page-no="sc.pageNo"
          @next-page="nextPage"
          @prev-page="prevPage"
          @direct-page="directPage"
        ></my-pagination>
      </nav>
    </template>
  </div>
</template>

<script>
import $ from "jquery";
import { searchTrainRefundOrders, showTrainOrderStatus } from "@/api/train.js";
import MyDatePicker from "@/components/my-datepicker.vue";
import MyPagination from "@/components/my-pagination.vue";

import MySelectCustomer from "@/components/my-select2-customer.vue";
import ModalExcelUpload from "@/components/modal/my-modal-excel-upload-train-orders.vue";

export default {
  components: {
    MyDatePicker,
    MyPagination,
    MySelectCustomer,
    ModalExcelUpload,
  },
  data() {
    return {
      dataList: [],
      searchOption: false,
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 25,
        pageTotal: 0,
      },
      beginDate: "",
      endDate: "",
      orderNo: "",
      ddate: "",
      name: "",
      customerId: -1,
      user: { id: 0 },

      status: -1,
    };
  },
  mounted: function () {
    this.search();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (loadingText) {
      this.$store.commit("showLoading", {
        loading: true,
        loadingText: loadingText,
      });
    },
    hideLoading: function () {
      this.$store.commit("showLoading", { loading: false });
    },
    showDetail: function (id) {
      this.$router.push("/h5/train/refund/order/" + id);
    },
    search: function () {
      this.showLoading();

      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        "sc.beginDate": this.beginDate,
        "sc.orderNo": this.orderNo,
        "sc.endDate": this.endDate,
        "sc.name": this.name,
        "sc.departureDate": this.ddate,
        customerId: this.customerId,
        "sc.status": this.status,
      };

      searchTrainRefundOrders(
        params,
        (v) => {
          this.dataList = v.dataList;
          this.sc = v.page;
        },
        () => {
          this.hideLoading();
        }
      );
    },
    importExcel: function () {
      this.$refs.modalUploadExcel
        .modal()
        .then((formData) => {
          this.showLoading("处理中......");
          uploadTrainOrders(
            formData.data,
            (v) => {
              if (v.status === "OK") {
                this.showErrMsg("操作成功");
              } else {
                this.showErrMsg("操作失败:" + v.errmsg, "danger");
              }
            },
            () => {
              this.hideLoading();
            }
          );
        })
        .catch((ex) => {});
    },
    getStatusDesc: function (status) {
      return showTrainOrderStatus(status);
    },
    reset: function () {
      this.customerId = -1;
      this.ddate = "";
      this.name = "";
      this.sc.pageNo = 1;
      this.beginDate = "";
      this.endDate = "";
      this.orderNo = "";
      this.status = -1;
    },
    getShortName: function (val, max) {
      if (val === null || val.length <= max) {
        return val;
      } else {
        return val.substring(0, max) + " ......";
      }
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>